package com.guomei.controller.curse;

import com.guomei.bean.curse.AddOrUpdateCourseChapter;
import com.guomei.bean.curse.CourseChapter;
import com.guomei.service.curse.CourseChapterService;
import com.guomei.utils.Result;
import com.guomei.utils.ValidationUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.time.LocalDateTime;

/**
 * 课程章节控制器
 */
@RestController
@RequestMapping("/api1/course-chapter")
@Slf4j
public class CourseChapterController {
    @Resource
    private CourseChapterService courseChapterService;

    /**
     * 创建新章节
     */
    @PostMapping("/createChapter")
    public Result<?> createChapter(@Valid @RequestBody AddOrUpdateCourseChapter addCourseChapter, BindingResult result) {
        log.info("创建/修改章节入参：{}", addCourseChapter);
        //实体类注解参数校验
        Result commonDataResponse = ValidationUtil.validInputParams(result);
        if (commonDataResponse != null) {
            return commonDataResponse;
        }
        try {
            CourseChapter courseChapter = new CourseChapter();
            courseChapter.setCourseId(addCourseChapter.getCourseId());
            courseChapter.setChapterName(addCourseChapter.getChapterName());
            courseChapter.setDescription(addCourseChapter.getDescription());
            courseChapter.setSortOrder(addCourseChapter.getSortOrder());
            courseChapter.setCreatedTime(LocalDateTime.now());
            courseChapter.setUpdatedTime(LocalDateTime.now());
            if (addCourseChapter.getId() == null){
                log.info("进行创建新章节");
                boolean success = courseChapterService.save(courseChapter);
                return success ? Result.success("创建新章节成功") : Result.fail("创建新章节失败");
            } else {
                log.info("进行更新章节");
                courseChapter.setId(addCourseChapter.getId());
                boolean success = courseChapterService.update(courseChapter);
                return success ? Result.success("更新章节成功") : Result.fail("更新章节失败");
            }
        } catch (Exception e) {
            log.info("创建新章节失败：" + e.getMessage());
            return Result.fail("创建新章节失败");
        }
    }

    /**
     * 根据条件查询章节
     */
    @PostMapping("/search")
    public Result<?> searchChapters(@RequestBody CourseChapter chapter) {
        log.info("根据条件查询章节：" + chapter);
        try {
            if (chapter == null || chapter.getPageNum() == null || chapter.getPageSize() == null){
                return Result.fail("分页字段信息缺失，查询失败");
            }
            
            // 验证分页参数是否有效
            if (chapter.getPageNum() <= 0 || chapter.getPageSize() <= 0) {
                return Result.fail("分页参数无效");
            }
            return courseChapterService.getByCondition(chapter);
        } catch (Exception e) {
            log.info("查询章节信息失败",e);
            return Result.fail("查询章节信息失败");
        }
    }

    @PostMapping("/deleteChapter")
    public Result<?> deleteChapter(@RequestBody CourseChapter chapter) {
        log.info("删除章节：" + chapter);
        try {
            if (chapter == null || chapter.getId() == null){
                return Result.fail("删除章节信息失败");
            }
            boolean success = courseChapterService.removeById(chapter.getId());
            return success ? Result.success("删除章节信息成功") : Result.fail("删除章节信息失败");
        } catch (Exception e) {
            log.info("删除章节信息失败",e);
            return Result.fail("删除章节信息失败");
        }
    }

}
